package test;

public class FalseSharingDemo2 implements Runnable {
    private static final int NUM_THREADS = 2;
    private static final int NUM_ITERATIONS = 100000000;

    private static SharedData2[] sharedDataArray = new SharedData2[NUM_THREADS];
    private final int index;

    public FalseSharingDemo2(int index) {
        this.index = index;
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < NUM_THREADS; i++) {
            sharedDataArray[i] = new SharedData2();
        }

        Thread[] threads = new Thread[NUM_THREADS];
        for (int i = 0; i < NUM_THREADS; i++) {
            threads[i] = new Thread(new FalseSharingDemo2(i));
        }

        long startTime = System.nanoTime();
        for (Thread thread : threads) {
            thread.start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        long endTime = System.nanoTime();
        System.out.println("Time taken: " + (endTime - startTime) + " ns");
    }

    @Override
    public void run() {
        for (int i = 0; i < NUM_ITERATIONS; i++) {
            sharedDataArray[index].value1 = i;
            sharedDataArray[index].value2 = i;
        }
    }
}




